@echo off
setlocal EnableExtensions EnableDelayedExpansion

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::	set
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
set cwd=%~dp0
set error=0
set YUI=%cwd%build\yuicompressor-2.4.7.jar
pushd %cwd%
call :process %*
popd
endlocal

exit /B 0

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::	checks if directory exist
::	sets error = 1 if path is not a directory
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:directory_exist
pushd %~fs1 2>nul
if errorlevel 1 (
	exit /B 1
) else (
	popd
	exit /B 0
)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::	checks if file exist
::	sets error = 1 if file do not exist or file is a directory
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:file_exist
if exist %~fs1 (
	call :directory_exist %1
	if errorlevel 1 (
		exit /B 0
	) else (
		exit /B 1
	)
) else (
	exit /B 1
)


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::	check files
::	check source file/directory existence
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:check_files
call :directory_exist %1
if errorlevel 1 (
	call :file_exist %1
	if errorlevel 1 (
		call :report_error invalid  source file %1
		exit /B 1
	)
)

call :directory_exist %2
if not errorlevel 1 (
	call :report_error target path %2 is a directory %errorlevel%
	exit /B 1
)

exit /B 0

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::	minify
::	direct minification of %1 source to %2 output
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:minify_file
type NUL > %2
java -jar %YUI% --disable-optimizations --preserve-semi --nomunge %1 > %2
exit /B 0

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::	minify directory
::	direct minification of %1 source to %2 output
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:minify_directory
set temp_file=%TEMP%\.source_concat.%type%
:: clear temp file
type NUL > %temp_file%

call :compact_directory %1 %temp_file%

:: minify tempfile
call :minify_file %temp_file% %2

del %temp_file%

exit /B 0

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::	minify using file list
::	minify using file list %1 sources to %2 output
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:minify_filelist
set temp_file=%TEMP%\.source_concat.%type%
:: clear temp file
type NUL > %temp_file%

call :compact_filelist %1 %temp_file% %3

:: minify tempfile
call :minify_file %temp_file% %2

del %temp_file%

exit /B 0

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::	compact files
::	direct minification of %1 source to %2 temp file. base %3
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:compact_filelist
call :file_exist %1
if errorlevel 1 (
	call :report_error file do not exist "%1" %~fs1
	exit /B 1
)
set base_dir=%~f3\
set base_dir=%base_dir:\=/%
for /f %%i in (%1) do (
	
	call :file_exist %base_dir%%%i
	if errorlevel 1 (
		call :report_error file do not exist! %base_dir%%%i
		exit /B 1
	) else (
		call :compact_fileset_filename %base_dir%%%i %2
	)
	
)
exit /B 0

:compact_fileset_filename
echo compacting %~fp1
type %~fs1 >> %~fs2

exit /B 0

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::	compact files
::	direct minification of %1 source to %2 temp file
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:compact_directory
:: iterate through files
pushd %~fs1
for /f %%i in ('dir /b /O-G') do (
	call :file_exist %%i
	if errorlevel 1 (
		call :compact_directory %%i %2
	) else (
		echo [compacting] %cd%\%%i ...
		type %%~fsi >> %~fs2
	)
)
popd
exit /B 0

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::	report error
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:report_error
echo [ERROR] %*
set error=1
exit /B 1

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::	main process
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:process

set mode=directory
set type=js

if /I %1==filelist (
	set mode=%1
	shift
)

if /I %1==css (
	set type=css
	shift
	
) else (
	:: js is default
	if /I %1==js (
		shift
	)
)

set source=%1
set target=%2

if [%3]==[] (
	set base_dir=%cwd%
) else (
	set base_dir=%3
)

if %mode%==filelist (
	call :minify_filelist %source% %target% %base_dir%
	
) else (
	call :check_files %source% %target%
	if not errorlevel 1 (
		call :file_exist %source%
		if not errorlevel 1 (
			call :minify_file %source% %target%
		) else (
			call :minify_directory %source% %target% %base_dir%
		)
	)
)

exit /B 0
